package com.gargi.crimepatrol.main;

import java.util.Vector;

import com.gargi.crimepatrol.network.CrimePatrolOperation;
import com.gargi.crimepatrol.network.CrimePatrolOperation.CrimePatrolDataListener;
import com.gargi.crimpatrol.utils.Constant;
import com.nokia.lwuit.components.HeaderBar;
import com.nokia.lwuit.components.InfiniteProgressIndicator;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;

public class VideoListView extends View 
						   implements CrimePatrolDataListener {
	
	LiveTvList m_crimepatrollist;
	InfiniteProgressIndicator m_progressbar;
	HeaderBar m_headerbar;

	public VideoListView(CrimePatrolMidlet midlet) {
		super(midlet);
		
		setTitle("Crime Patrol");
		addCommands();
		initComponents();
		
	}
	
	public void show() {
		super.show();
		sendCrimePatrolRequest();
	}
	
	private void add_component(Component comp) {
		if(!contains(comp)) {
			addComponent(comp);
		}
	}
	
	private void initComponents() {
		m_crimepatrollist = new LiveTvList(this);
		m_progressbar = new InfiniteProgressIndicator(InfiniteProgressIndicator.SPINNER_LARGE);
		m_headerbar = new HeaderBar("Crime Patrol", true);
		
		add_component(m_headerbar);
		
	}
	
	private void sendCrimePatrolRequest() {
		CrimePatrolOperation crimePatrolOps = new CrimePatrolOperation(this);
		crimePatrolOps.start();
	}
	
	private void addCommands() {
		Command abtCommand = new Command("about");
		Command helpCommand = new Command("help");
		
		addCommand(abtCommand);
		addCommand(helpCommand);
	}

	public void onCrimeResponseReceived(Vector responseVector) {
		Constant.log("Got Correct Response !!");
		
		addListViewComp(responseVector);
	}

	public void onErrorReceived() {
		Constant.log("Got Error Response");
	}
	
	private void addListViewComp(Vector responsevector) {
		
		add_component(m_crimepatrollist);	
						
		m_crimepatrollist.setImageTextButtonRenderer();
		m_crimepatrollist.setCategory(responsevector);
		
		m_headerbar.removeSpinner();
		
		repaint();
	}

}
